import { wxRequest } from "../../utils/httpRequest"
// import initComputed from 'wx-computed'
import utils from "../../utils/util"
import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';

const regeneratorRuntime = require('../../utils/runtime')

Page({
    data: {
        shopId: "",
        orderId: null,
        nowUserInfo: null,
        mealObj: null,
        shopName: "",
        columns: [],
        dialogTitle: "",
        fieldValue: "",
        showChooseDialogFlag: false,
        address: null,
        phone: null,
        realName: null,
        sex: null,
        remarks: null,
        tablewareNum: 0,
        allNeedMoney: 0
    },
    getNowUserInfo() {
      return new Promise((resolve, reject) => {
        wxRequest('getOneUserInfo', {}).then((res) => {
            if (res.code == 10000) {
              this.setData({ nowUserInfo: res.data });
              if (!this.data.address) { this.setData({ address: res.data.address[0] }); }
              if (!this.data.remarks) { this.setData({ remarks: res.data.remarks[0] }); }
              if (!this.data.phone) { this.setData({ phone: res.data.userPhone }); }
              if (!this.data.realName) { this.setData({ realName: res.data.userRealName }); }
              if (!this.data.sex) { this.setData({ sex: res.data.userSex }); }
              resolve();
            } else {
              reject();
            }
        })
      })
    },
    getOrderInfo(id) {
      this.setData({ orderId: id });
      let obj ={
        orderId: id
      }
      wxRequest('getOrderByOrderId', obj).then((res) => {
        if (res.code == 10000) {
          this.setData({ mealObj: res.data });
        }
      });
    },
    changePickerValue(event) {
        const { picker, value, index } = event.detail;
        if (this.data.dialogTitle == "地址") {
            this.setData({ address: value });
        } else if (this.data.dialogTitle == "备注") {
          this.setData({ remarks: value });
        } else if (this.data.dialogTitle == "餐具") {
          this.setData({ tablewareNum: value });
        }
    },
    addressShowDialog() {
        this.setData({ showChooseDialogFlag: true });
        this.setData({ dialogTitle: "地址" });
        this.setData({ columns: this.data.nowUserInfo.address })
    },
    remarksShowDialog() {
      this.setData({ showChooseDialogFlag: true });
      this.setData({ dialogTitle: "备注" });
      this.setData({ columns: this.data.nowUserInfo.remarks })
    },
    tablewareNumShowDialog() {
      this.setData({ showChooseDialogFlag: true });
      this.setData({ dialogTitle: "餐具" });
      this.setData({ columns: [0, 1, 2, 3] })
    },
    // dialog输入框的变换
    fieldValueChange(event) {
        this.setData({ fieldValue: event.detail });
    },
    fieldClose() {
        this.setData({ dialogTitle: "" });
        this.setData({ showAddDialogFlag: false });
    },
    doField() {
      if (this.data.dialogTitle == "地址") {
        this.doAddAddress()
      } else if (this.data.dialogTitle == "联系姓名") {
        this.doChangeRealName();
      } else if (this.data.dialogTitle == "联系电话") {
        this.doChangePhone();
      } else if (this.data.dialogTitle == "备注") {
        this.doAddRemarks();
      }
    },
    addNew() {
      this.setData({ fieldValue: "" });
      this.setData({ showChooseDialogFlag: false });
      this.setData({ showAddDialogFlag: true });
    },
    // 与后端交互添加地址信息
    doAddAddress() {
      let url = "doAddAddress";
      let params = {
          updateValue: this.data.fieldValue
      }
      wxRequest(url, params).then((res) => {
          if (res.code == 10000) {
              Notify({ type: 'success', message: '添加成功' });
              this.getNowUserInfo().then(() => {
                this.setData({ address: this.data.fieldValue })
                this.fieldClose();
              });
          }
      })
    },
    // 与后端交互添加备注信息
    doAddRemarks() {
      let url = "doAddRemarks";
      let params = {
          updateValue: this.data.fieldValue
      }
      wxRequest(url, params).then((res) => {
          if (res.code == 10000) {
              Notify({ type: 'success', message: '添加成功' });
              this.getNowUserInfo().then(() => {
                this.setData({ remarks: this.data.fieldValue })
                this.fieldClose();
              });
          }
      })
    },
    // 修改联系姓名
    changeRealName(event) {
      this.setData({ fieldValue: event.currentTarget.dataset.originalvalue });
      this.setData({ dialogTitle: "联系姓名" });
      this.setData({ showAddDialogFlag: true });
    },
    // 与后端交互，修改联系姓名
    doChangeRealName() {
      let url = "doChangeRealName";
      let params = {
          updateValue: this.data.fieldValue
      }
      wxRequest(url, params).then((res) => {
          if (res.code == 10000) {
              Notify({ type: 'success', message: '修改姓名成功' });
              this.getNowUserInfo().then(() => {
                this.setData({ realName: this.data.fieldValue })
                this.fieldClose();
              });
          }
      })
    },
    // 修改联系姓名
    changePhone(event) {
      this.setData({ fieldValue: event.currentTarget.dataset.originalvalue });
      this.setData({ dialogTitle: "联系电话" });
      this.setData({ showAddDialogFlag: true });
    },
    // 与后端交互修改联系电话
    doChangePhone() {
      let url = "doChangePhone";
      let params = {
          updateValue: this.data.fieldValue
      }
      wxRequest(url, params).then((res) => {
        if (res.code == 10000) {
            Notify({ type: 'success', message: '修改手机电话成功' });
            this.getNowUserInfo().then(() => {
              this.setData({ phone: this.data.fieldValue })
              this.fieldClose();
            });
        }
      })
    },
    perfectOrder() {
        let obj = {
            shopId: this.data.shopId,
            orderId: this.data.orderId,
            userPhone: this.data.phone,
            address: this.data.address,
            tablewareNum: this.data.tablewareNum,
            remarks: this.data.remarks
        }
        let url = "perfectOrder";
        wxRequest(url, obj).then((res) => {
            if (res.code == 10000) {
                Dialog.alert({
                  title: '提示',
                  message: '下单成功,你可以在历史订单页查看进度',
                }).then(() => {
                  wx.switchTab({
                      url: '/pages/historyOrder/historyOrder',
                  })
                });
            }
        })
    },
    onLoad(options) {
        this.setData({shopName: options.shopName})
        this.setData({shopId: options.shopId})
        this.getNowUserInfo();
        this.getOrderInfo(options.id);
    },
})